//
//  HomeView.swift
//  FreeWallpaperSwiftUI
//
//  Created by 张少康 on 2023/11/28.
//

import SwiftUI

struct HomeView: View {
    
    @Namespace private var namespace
    
    @ObservedObject var viewModel = HomeViewModel()
    
    @State var showModel: WallpaperItemModel? = nil
    
    @State var showMagicView: Bool = false
    
    @State var showDailyView: Bool = false
    
    var body: some View {
        ZStack {
            ScrollView(.vertical) {
                ScrollViewReader { proxy in
                    VStack {
                        HStack {
                            Text("今日壁纸").font(.custom("GB18030Bitmap", size: 40)).fontWeight(.bold)
                            Spacer()
                            Button(action: {
                                self.showMagicView = true
                            }, label: {
                                Text("随便来一张").foregroundStyle(.blue).font(.callout)
                            })
                        }
                        .frame(height: 64)
                        .padding(.horizontal)
                        .id(0)
                        
                        LazyVStack(spacing: 0.0) {
                            ForEach(viewModel.wallpaperItems.indices, id: \.self) { index in
                                let model = viewModel.wallpaperItems[index]
                                WallpaperItemView(model: model).matchedGeometryEffect(id: (model.url ?? ""), in: namespace)
                                    .onTapGesture {
                                        withAnimation {
                                            self.showModel = model
                                    }
                                }
                            }
                        }
                        if viewModel.wallpaperItems.count > 0 {
                            VStack{
                                HStack {
                                    Text("什么，没有喜欢的？")
                                    Button(action: {
                                        viewModel.changeItems()
                                    }, label: {
                                        Text("换一换").foregroundStyle(.blue).font(.callout)
                                    })
                                }.padding(.vertical)
                            }
                            
                            HStack {
                                Button(action: {
                                    self.showDailyView = true
                                }, label: {
                                    Text("不开心吗？听我一句劝。").foregroundStyle(.blue).font(.callout)
                                })
                            }
                            .frame(height: 44)
                            .padding(.horizontal)
                        }

                    }.onChange(of: viewModel.scrollToTop, perform: { newValue in
                        if viewModel.scrollToTop {
                            proxy.scrollTo(0, anchor: .top)
                            viewModel.scrollToTop = false
                        }
                    })
                }
                
            }
            
            if showModel != nil {
                PreviewImageView(namespace: namespace,model: showModel!)
                    .onTapGesture {
                        self.showModel = nil
                }
            }
            
            if self.showMagicView {
                MagicImageView(showMagicView: $showMagicView, allImageModel: viewModel.allItems)
            }
    
            if self.showDailyView {
                DailySentenceView(showDailyView: $showDailyView)
            }
        }
    }
 
}

#Preview {
    HomeView()
}
